package com.spring.cloud.examples.es.service.impl;

import com.spring.cloud.examples.es.entity.Product;
import com.spring.cloud.examples.es.service.ProductESService;
import io.searchbox.client.JestClient;
import io.searchbox.core.Bulk;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductESServiceImpl implements ProductESService {

    @Autowired
    private JestClient jestClient;


    public void save(Product product) {
        Bulk.Builder bulk = new Bulk.Builder();
        Index index = new Index.Builder(product).index("product_index").type("product_type").build();
        try {
            bulk.addAction(index);

            jestClient.execute(bulk.build());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public List<Product> queryByKeyword(String keyword) {


        //高亮
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        //商品名和类别高亮
        highlightBuilder.field("name");
        highlightBuilder.preTags("<font color='red'>").postTags("</font>");//高亮标签
        highlightBuilder.fragmentSize(500);//高亮内容长度

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.highlighter(highlightBuilder);

        searchSourceBuilder.query(QueryBuilders.multiMatchQuery(keyword,"name","category","brands")).from(0).size(100); //查询0,10 前100条

        Search search = new Search.Builder(searchSourceBuilder.toString())
                .addIndex("product_index").addType("product_type").build();

        List<Product> products = new ArrayList<Product>();
        try {
            SearchResult result  = jestClient.execute(search);
            if(result.getResponseCode()!=200)
            {
                return null;
            }
            List<SearchResult.Hit<Product,Void>> hits= result.getHits(Product.class);
            for(SearchResult.Hit<Product, Void> hit : hits)
            {
                Product source = hit.source;
                //获取高亮后的内容
                Map<String, List<String>> highlight = hit.highlight;
                if (highlight!=null){
                    List<String> name = highlight.get("name");//高亮后的name
                    if(name!=null){
                        source.setName(name.get(0));
                    }
                }
                Product product = new Product();
                product.setName(source.getName());
                product.setCategory(source.getCategory());
                product.setBrands(source.getBrands());
                products.add(product);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return products;
    }

}
